package main

import (
	"io"
	"io/ioutil"
	"log"
	"os/exec"
)

func tsProducer(writer *io.PipeWriter) {
	//将本地的ts文件 写入 writer
	for i := 0; i < 1; i++ {
		tsName := "/root/01.mp4"
		content, err := ioutil.ReadFile(tsName)
		log.Print("ts produce loop:", tsName)

		if err != nil {
			log.Fatal(err)
		}

		n, err := writer.Write(content)
		if err != nil {
			log.Print("write to pipe failed:", err.Error())
		}

		log.Printf("write to pipe content len:%d, success\n", n)
	}
}

func main() {
	rp, wp := io.Pipe()
	go tsProducer(wp)

	for {
		log.Print("main loop")
		cmd := exec.Command("ffmpeg", "-re", "-i", "pipe:0", "-vcodec", "copy", "-acodec", "copy", "-f", "flv", "rtmp://127.0.0.1/live/1")
		cmd.Stdin = rp

		log.Print("cmd run")
		err := cmd.Run()
		log.Printf("Command finished with error: %v", err)
	}
}
